package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.settings.dao.UserDao;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.utils.SqlSessionUtil;
import com.bjpowernode.crm.vo.PaginationVO;
import com.bjpowernode.crm.workbench.dao.ActivityDao;
import com.bjpowernode.crm.workbench.dao.ActivityRemarkDao;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.ActivityRemark;
import com.bjpowernode.crm.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: crm
 * @description:
 * @author: 玉树临风的寒
 * @create: 2020-12-16 19:08:28
 **/
public class ActivityServiceImpl implements ActivityService {
    private ActivityDao activityDao= SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao activityRemarkDao= SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    private UserDao userDao= SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public boolean save(Activity a) {
        boolean flag=true;
        int count=activityDao.save(a);
        if(count!=1){
            //添加失败
            flag=false;
        }
        return flag;
    }

    public PaginationVO<Activity> pageList(Map<String, Object> map) {

        //取得total
        int total =activityDao.getTotalByCondition(map);
        //取得dataList
        List<Activity> dataList=activityDao.getActivityListByCondition(map);
        //将total和dataList封装到vo中
        PaginationVO<Activity> vo = new PaginationVO<Activity>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回

        return vo;
    }

    public boolean delete(String[] ids) {
        boolean flag = true;
        //先查询出需要删除的备注的数量
        int count1 =activityRemarkDao.getCountByAids(ids);
        //删除备注 返回收到影响的条数(实际删除的数量)
        int count2=activityRemarkDao.deleteByAids(ids);
        if(count1!=count2){
            flag=false;
        }
        //删除市场活动
        int count3=activityDao.delete(ids);
        if(ids.length!=count3){
            flag=false;
        }

        return flag;

    }

    public Map<String, Object> getUserListAndActivity(String id) {
        System.out.println("进入 impl======getUserListAndActivity");
        //取uList
        List<User> uList=userDao.getUserList();
        //取a
        Activity a = activityDao.getById(id);
        //将uList和a打包成map
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("uList",uList);
        map.put("a",a);
        //返回map
        return map;
    }

    public boolean update(Activity a) {
        boolean flag=true;
        int count=activityDao.update(a);
        if(count!=1){
            //添加失败
            flag=false;
        }
        return flag;
    }

    public Activity detail(String id) {

        Activity a =activityDao.detail(id);

        return a;
    }

    public List<ActivityRemark> getRemarkListByAid(String activityId) {
        List<ActivityRemark> arList =activityRemarkDao.getRemarkListByAid(activityId);

        return arList;
    }

    public boolean deleteRemark(String id) {
        boolean flag = true;

        int count = activityRemarkDao.deleteById(id);
        if (count!=1) {
            flag=false;
        }

        return flag;
    }

    public boolean saveRemark(ActivityRemark ar) {
        boolean flag = true;
        int count =activityRemarkDao.saveRemark(ar);
        if(count!=1){
            flag=false;
        }
        return flag;
    }

    public boolean updateRemark(ActivityRemark ar) {
        boolean flag = true;
        int count =activityRemarkDao.updateRemark(ar);
        if(count!=1){
            flag=false;
        }
        return flag;
    }

    public List<Activity> getActivityListByClueId(String clueId) {
        List<Activity> aList= activityDao.getActivityListByClueId(clueId);

        return aList;
    }

    public List<Activity> getActivityListByNameAndNotByClueId(Map<String, String> map) {
        List<Activity> aList =activityDao.getActivityListByNameAndNotByClueId(map);

        return aList;
    }

    public List<Activity> getActivityListByName(String aname) {

        List<Activity> aList = activityDao.getActivityListByName(aname);

        return aList;
    }

    public Map<String, Object> getCharts() {

        //取得total
        int total=activityDao.getTotal();

        //取得dataList
        List<Map<String,Object>> dataList=activityDao.getCharts();

        //取得所有名字
        List<String> names = activityDao.getNames();
        //将total和dataList保存到map中
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("dataList",dataList);
        map.put("total",total);
        map.put("names",names);
        //返回map
        return map;
    }


}
